using UnityEngine;

public class MapViewCar : MonoBehaviour
{
	private float minX = -2400f;

	private float maxX = 2400f;

	private float minY = 546f;

	private float maxY = 643f;

	private bool moveRight;

	private bool isTop;

	private float curX;

	private float curY;

	private float speed;

	private float angle;

	private Transform parent;

	private MapViewCarManager manager;

	private Transform leftTyre;

	private Transform rightTyre;

	private void Start()
	{
		((RectTransform)base.transform).anchoredPosition = new Vector3(curX, curY);
		base.transform.SetParent(parent);
		if (isTop)
		{
			base.transform.localScale = new Vector3(-1f, 1f, 1f);
		}
		else
		{
			base.transform.localScale = Vector3.one;
		}
		leftTyre = base.transform.Find("LeftTyre");
		rightTyre = base.transform.Find("RightTyre");
	}

	private void Update()
	{
		if (moveRight)
		{
			curX += Time.deltaTime * speed;
			if (curX > maxX)
			{
				manager.RemoveCar(isTop, this);
			}
			((RectTransform)base.transform).anchoredPosition = new Vector3(curX, curY);
		}
		else
		{
			curX -= Time.deltaTime * speed;
			if (curX < minX)
			{
				manager.RemoveCar(isTop, this);
			}
			((RectTransform)base.transform).anchoredPosition = new Vector3(curX, curY);
		}
		angle += Time.deltaTime * speed;
		SetTyreRotate(angle);
	}

	private void SetTyreRotate(float angle)
	{
		if (!isTop)
		{
			leftTyre.eulerAngles = new Vector3(0f, 0f, angle);
			rightTyre.eulerAngles = new Vector3(0f, 0f, angle);
		}
		else
		{
			leftTyre.eulerAngles = new Vector3(0f, 0f, 0f - angle);
			rightTyre.eulerAngles = new Vector3(0f, 0f, 0f - angle);
		}
	}

	public void InitCar(MapViewCarManager manager, bool isTop, bool isRandomPosition, Transform parent)
	{
		this.isTop = isTop;
		this.manager = manager;
		this.parent = parent;
		if (isTop)
		{
			curY = maxY;
			if (isRandomPosition)
			{
				curX = Random.Range(minX, minY);
			}
			else
			{
				curX = minX - (float)Random.Range(0, 500);
			}
			moveRight = true;
		}
		else
		{
			curY = minY;
			if (isRandomPosition)
			{
				curX = Random.Range(minX, minY);
			}
			else
			{
				curX = maxX + (float)Random.Range(0, 500);
			}
			moveRight = false;
		}
		speed = Random.Range(70, 140);
	}
}
